var currentPage;//当前页数
var count;	//总记录数
var pageSize;	//每一页记录数
var totalPage;	//总页数

//获取分页数据
function turnPage(page){
	$.ajax({
        type:'POST',
        url:'/citrusmonitor/expert.do?method=findExperts',
        data:{
            "currentPage": page,
            "pageSize": 10
        },
        beforeSend:function(){
            $("#data-area ul").append("加载中...");
        },
        success:function(data){
        	data = JSON.parse(data)
            $("#data-area ul").empty();//移出原来的分页数据
            count = data.count;
            pageSize = data.pageSize;
            currentPage = page;
            totalPage = data.totalPage;

            var html = '';
            $.each(data.beanList,function(i,value){
                html +="<tr><td><input type=\"checkbox\" value="+value["expertId"]+" name='subChk'><input type=\"hidden\" value="+value["expertId"]+"></td>"
        			  		+"<td>"+(i+1)+"</td>"
        			          +"<td>"+value["expertName"]+"</td>"
        			          + "<td>"+value["password"]+"</td>"
        			          +"<td>"+value["workField"]+"</td>"
        			          + "<td>"+value["introduction"]+"</td>"
                              +" <td>"+value["workPlace"]+"</td>"
                              +" <td>"+value["email"]+"</td>"
        			          +"<td>"+value["registerTime"]+"</td>"
        			          +" <td>"
        			          +"</span id=\"edit\"><span><a href=\"edit.html?expertId="+value["expertId"]+"\" target=\"rightframe\">"+"<i class=\"iconfont\">&#xe7e0;</i></a></span>"
        			          +"<span class=\"delete\"  onclick='deleteBtn(this)'><i class=\"iconfont\">&#xe695;</i></span>"
        			          +"</td></tr>"
              });
            
            var tbody = document.getElementById("tbody");
            tbody.innerHTML = html;
        },

        complete:function(){    //添加分页按钮栏
            getPageBar();
        },
        error:function(){
            alert("数据加载失败");
        }
    })
}

//获取分页条
function  getPageBar(){
    if(currentPage > totalPage){
        currentPage = totalPage;
    }
    if(currentPage < 1){
        currentPage = 1;
    }
    pageBar = "";

    //如果不是第一页
    if(currentPage != 1){
    	pageBar += "<span class='pageBtn'><a href='javascript:turnPage(1)'>首页</a></span>";
        pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+(currentPage-1)+")'><<</a></span>";
    }

    //显示页码按钮（5个）
    var start,end;
    if(totalPage <= 5){
        start = 1;
        end = totalPage;
    }else{
        if(currentPage -2<=0){
            start = 1;
            end =5;
        }else{
            if(totalPage -currentPage <2){
                start = totalPage -4;
                end =totalPage;
            }else{
                start = currentPage -2;
                end = currentPage +2;
            }
        }
    }

    for(var i = start;i<=end;i++){
        if(i == currentPage){
            pageBar +="<span class='pageBtn-selected'><a href='javascript:turnPage("+i+")'>"+i+"</a></span>"
        }else{
            pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+i+")'>"+i+"</a></span>";
        }
    }

    //如果不是最后一页
    if(currentPage !=totalPage){
        pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+(parseInt(currentPage)+1)+")'>>></a></span>";
        pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+totalPage+")'>尾页</a></span>";
    }
    $("#pageBar").html(pageBar);
}

//页面加载时初始化分页
$(function() {
    turnPage(1);
  });